<template>
  <BasicModal
    v-bind="$attrs"
    :width="600"
    :title="'编辑订单'"
    :help-message="'edit order'"
    @register="register"
    @ok="handleOk"
  >
    <a-form
      ref="formRef"
      :model="state.formState"
      :rules="rules"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 20 }"
    >
      <a-form-item label="订单编号" name="order_number">
        <a-input v-model:value="state.formState.order_number" placeholder="请输入订单数量" />
      </a-form-item>
      <a-form-item label="订单价格" name="order_price">
        <a-input v-model:value="state.formState.order_price" placeholder="请输入订单价格" />
      </a-form-item>
      <a-form-item label="支付状态" name="pay_status">
        <a-radio-group v-model:value="state.formState.pay_status" :options="payStatusOptions" />
      </a-form-item>
      <a-form-item label="订单支付方式" name="order_pay">
        <a-radio-group v-model:value="state.formState.order_pay" :options="orderPayOptions" />
      </a-form-item>
      <a-form-item label="订单是否发货" name="is_send">
        <a-radio-group v-model:value="state.formState.is_send" :options="isSendOptions" />
      </a-form-item>
    </a-form>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { message } from 'ant-design-vue';
  import { reactive, ref, watch } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useShopOrdersStore } from '/@/store/modules/shopOrdersStore';
  import { deepClone } from '/@/utils/common';

  const shopOrdersStore = useShopOrdersStore();

  const [register, { closeModal, getVisible }] = useModalInner();

  const formRef = ref();

  const emits = defineEmits(['editResult']);

  const props = defineProps({
    editRow: {
      type: Object,
      default: () => {
        return {};
      },
    },
  });

  const state = reactive({
    formState: {
      order_id: 0,
      order_number: '',
      order_price: '',
      pay_status: '',
      order_pay: '',
      is_send: '',
    },
  });

  const payStatusOptions = [
    { label: '未付款', value: '0' },
    { label: '已付款', value: '1' },
  ];
  const orderPayOptions = [
    { label: '未支付', value: '0' },
    { label: '支付宝', value: '1' },
    { label: '微信', value: '2' },
    { label: '银行卡', value: '3' },
  ];
  const isSendOptions = [
    { label: '未发货', value: '0' },
    { label: '已发货', value: '1' },
  ];

  const rules = {
    cat_name: [{ required: true, message: '请输入参数名称', trigger: 'blur' }],
  };

  watch(
    () => getVisible?.value,
    (visible) => {
      if (!visible) {
        formRef.value.resetFields();
      }
    },
  );

  watch(
    () => props.editRow,
    (newData) => {
      for (const key in newData) {
        Object.keys(state.formState).find((item) => {
          if (item === key) {
            if (key === 'is_send') {
              state.formState[key] = newData[key] == '否' ? '0' : '1';
            } else {
              state.formState[key] = newData[key];
            }
          }
        });
      }
    },
  );

  const editOrderData = async () => {
    try {
      const params = deepClone(state.formState);
      params['id'] = params['order_id'];
      delete params['order_id'];
      const data = await shopOrdersStore.updateOrder(params);
      emits('editResult', data);
      message.success('修改成功');
    } catch (error) {
      console.error(error);
    }
  };

  const handleOk = () => {
    formRef.value
      .validate()
      .then(async () => {
        editOrderData();
        closeModal();
      })
      .catch((error: any) => {
        console.error(error);
      });
  };
</script>

<style scoped></style>
